package cn.edu.scau.dormitory_management_system.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import cn.edu.scau.dormitory_management_system.entity.RegisterEntity;
import cn.edu.scau.dormitory_management_system.entity.RepairEntity;
import cn.edu.scau.dormitory_management_system.service.impl.RepairServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 维修表
 *
 * @author wanglingfu
 * @email 1006340605@qq.com
 * @date 2021-08-02 14:35:10
 */
@RestController
@RequestMapping("/admin/repair")
public class RepairController {

    @Autowired
    private RepairServiceImpl repairService;


    //提供给后勤人员，
    // 1.维修开始时修改状态，维修中。
    // 2.维修完成后修改状态，把未受理改成已维修。
    @RequestMapping("/adminUpdateRepair")
    public Object adminUpdateRepair(@RequestBody RepairEntity repairEntity){
        JSONObject object = new JSONObject();
        if (repairEntity.getStatus().equals("未受理")){
            repairEntity.setStatus("维修中");
        }else if(repairEntity.getStatus().equals("维修中")){
            repairEntity.setStatus("已完成");
        }else if(repairEntity.getStatus().equals("已完成")){
            object.put("code",0);
            object.put("msg","状态已完全,无法修改");
            return object;
        }else{
            repairEntity.setStatus("维修中");
        }
        boolean b = this.repairService.updateRepair(repairEntity);
        if(b){
            object.put("code",200);
            object.put("msg","修改成功");
        }else{
            object.put("code",0);
            object.put("msg","修改失败");
        }
        return object;
    }

    //提供给后勤人员，根据宿舍查询维修进度。
    @RequestMapping("/adminSelectRepair")
    public Object adminSelectRepair(@RequestParam(defaultValue = "1") int current,
                                  @RequestParam String dorm,
                                  int size){
        JSONObject object = new JSONObject();

        IPage<RepairEntity> page =new Page<>(current,size);
        QueryWrapper<RepairEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("repair_id");
        wrapper.like("dorm_name",dorm);
        page = repairService.page(page, wrapper);
        boolean b = page.getRecords().isEmpty();
        if(b){
            object.put("code",0);
            object.put("msg","无维修数据");
        }else{
            object.put("code",200);
            object.put("page",page);
        }
        return object;
    }

    //根据tel查询报修进度。提供给学生使用。
    @RequestMapping("/stuSelectRepair")
    public Object stuSelectRepair(@RequestParam(defaultValue = "1") int current,
                                  @RequestParam String tel,
                               int size){
        JSONObject object = new JSONObject();

        IPage<RepairEntity> page =new Page<>(current,size);
        QueryWrapper<RepairEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("repair_id");
        wrapper.eq("tel",tel);
        page = repairService.page(page, wrapper);
        boolean b = page.getRecords().isEmpty();
        if(b){
            object.put("code",0);
            object.put("msg","无维修数据");
        }else{
            object.put("code",200);
            object.put("page",page);
        }
        return object;
    }

    //提供一个给学生报修的插入方法。
    @RequestMapping("/stuInsertRepair")
    public Object stuInsertRepair(@RequestParam("tel") String tel,
                                  @RequestParam("dormName")String dormName,
                                  @RequestParam("type")String type,
                                  @RequestParam("content")String content){//报修内容

        JSONObject object = new JSONObject();

        boolean b = repairService.stuInsertRepair(tel,dormName,type,content)>0;

        if(b){
            object.put("code",200);
            object.put("msg","插入成功");
        }else{
            object.put("code",0);
            object.put("msg","操作失败");
        }
        return object;
    }

    @RequestMapping("/selectRepair")
    public Object selectRepair(@RequestParam(defaultValue = "1") int current,
                               int size){
        JSONObject object = new JSONObject();

        IPage<RepairEntity> page =new Page<>(current,size);
        QueryWrapper<RepairEntity> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("repair_id");
        page = repairService.page(page, wrapper);
        boolean b = page.getRecords().isEmpty();
        if(b){
            object.put("code",0);
            object.put("msg","无维修数据");
        }else{
            object.put("code",200);
            object.put("page",page);
        }
        return object;
    }
    @RequestMapping("/insertRepair")
    public Object insertRepair(@RequestBody RepairEntity repairEntity){//报修内容

        JSONObject object = new JSONObject();

        boolean b = repairService.insetRepair(repairEntity);
        if(b){
            object.put("code",200);
            object.put("msg","插入成功");
        }else{
            object.put("code",0);
            object.put("msg","操作失败");
        }
        return object;
    }

}
